package com.jackstraw.create.controller;

import com.jackstraw.create.param.RespResult;
import com.jackstraw.create.pojo.DatabaseConnectInfo;
import com.jackstraw.create.pojo.TableData;
import com.jackstraw.create.utils.DatabaseUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 版权所有： 稻草人
 * 所在包：com.jackstraw.create.controller
 * 文件名：
 * 项目：create
 * 功能描述：
 * 修改时间：2021-04-16 18:00
 *
 * @author jackstraw
 */
@RestController
@RequestMapping(value="/database")
public class DatabaseController {

    /**
     * 初始化数据库连接
     * @param databaseConnectInfo
     * @return
     */
    @RequestMapping(value="/initConnect")
    public RespResult initConnect(DatabaseConnectInfo databaseConnectInfo) {
        try {
            DatabaseUtil.initConnection(databaseConnectInfo);
            return RespResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return RespResult.error(e.getMessage());
        }
    }

    /**
     * 获取连接数据库中列表
     * @return
     */
    @RequestMapping(value="/getTableList")
    public RespResult getTableList() {
        RespResult result;
        try {
            List<TableData> tableList = DatabaseUtil.getTableList();
            result = RespResult.success();
            result.setData(tableList);
        } catch (Exception e) {
            e.printStackTrace();
            result = RespResult.error();
            result.setMsg(e.getMessage());
        }
        return result;
    }
}
